package summer.hot100.v1;

import java.util.*;

public class Solution49 {
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> map = new HashMap<>();
        for (String s : strs) {
            int[] hash = new int[26];
            for (int i = 0; i < s.length(); i++) {
                hash[s.charAt(i) - 'a']++;
            }
            StringBuilder key = new StringBuilder();
            for (int i = 0; i < 26; i++) {
                if (hash[i] != 0) key.append((char) (i + 'a')).append(hash[i]);
            }
            List<String> list = map.getOrDefault(key.toString(), new ArrayList<>());
            list.add(s);
            map.put(key.toString(), list);
        }
        return new LinkedList<>(map.values());
    }
}
